﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using WordServices.HttpService;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace WordsOfLove.ForumPages
{
	/// <summary>
	/// An empty page that can be used on its own or navigated to within a Frame.
	/// </summary>
	public sealed partial class PostEditPage : Page
	{
		public ForumPostInfo InitialPost { get; set; }

		public PostEditPage()
		{
			this.InitializeComponent();
		}

		protected override void OnNavigatedTo(NavigationEventArgs e)
		{
			base.OnNavigatedTo(e);
			if (e.Parameter is ForumPostInfo)
			{
				InitialPost = e.Parameter as ForumPostInfo;
				HttpUtils utils = HttpUtilsFactory.Utils;
				List<ForumPostTag> tags = utils.SendGetJsonMessage<List<ForumPostTag>>($"api/ForumPost/GetTags?postId={InitialPost.Id}");
				foreach(ForumPostTag tag in tags)
				{
					Button btn = new Button() { Content = tag.Content };
					btn.Click += (S, E) => { TagGrid.Children.Remove(btn); };
					TagGrid.Children.Add(btn);
				}
				TitleBox.Text = InitialPost.Title;
				ContentBox.TextDocument.SetText(Windows.UI.Text.TextSetOptions.None, InitialPost.Content);
			}
			else InitialPost = null;
		}

		private async void SaveBtn_Click(object sender, RoutedEventArgs e)
		{
			HttpUtils utils = HttpUtilsFactory.Utils;
			ContentBox.TextDocument.GetText(Windows.UI.Text.TextGetOptions.UseCrlf, out string contentString);
			if (InitialPost == null)
			{
				try
				{
					ForumPost post = utils.SendPostAndReturnJsonMessage<ForumPost>("api/ForumPost/CreatePost", new ForumPost()
					{
						CreateTime = DateTime.Now,
						UpdateTime = DateTime.Now,
						Title = TitleBox.Text,
						Content = contentString,
						UserId = CurrentUserFactory.CurrentUserId
					});
					foreach(object o in TagGrid.Children)
					{
						Button btn = o as Button;
						utils.SendPutMessage("api/ForumPost/CreateTag", new ForumPostTag()
						{
							Content = btn.Content as string,
							PostId = post.Id
						});
					}
					if (Frame.CanGoBack) Frame.GoBack();
				}
				catch //(Exception ex)
				{
					await new MessageDialog("服务器错误").ShowAsync();
				}
			}
			else
			{
				try
				{
					InitialPost.UpdateTime = DateTime.Now;
					InitialPost.Title = TitleBox.Text;
					InitialPost.Content = contentString;
					utils.SendPutMessage("api/ForumPost/UpdatePost", InitialPost);
					utils.SendDeleteMessage($"api/ForumPost/DeleteAllTag?postId={InitialPost.Id}");
					foreach (object o in TagGrid.Children)
					{
						Button btn = o as Button;
						utils.SendPutMessage("api/ForumPost/CreateTag", new ForumPostTag()
						{
							Content = btn.Content as string,
							PostId = InitialPost.Id
						});
					}
					if (Frame.CanGoBack) Frame.GoBack();
				}
				catch //(Exception ex)
				{
					await new MessageDialog("服务器错误").ShowAsync();
				}
			}
		}

		private void CancelBtn_Click(object sender, RoutedEventArgs e)
		{
			if (Frame.CanGoBack) Frame.GoBack();
		}

		private void AddTagBtn_Click(object sender, RoutedEventArgs e)
		{
			if(TagBox.Text != "")
			{
				Button btn = new Button() { Content = TagBox.Text };
				btn.Click += (S, E) => { TagGrid.Children.Remove(btn); };
				TagGrid.Children.Add(btn);
			}
		}
	}
}
